package cn.nexd.app.collector.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import cn.nexd.app.collector.R;
import cn.nexd.app.collector.presenter.splash.LoadDataContract;
import cn.nexd.app.collector.presenter.splash.LoadDataPresenter;
import cn.nexd.app.collector.util.AnimUtil;
import cn.nexd.app.collector.util.ScreenUtils;
import cn.nexd.app.collector.util.UMengUtil;
import cn.nexd.utils.framework.tools.Logger;


public class SplashActivity extends AppCompatActivity implements ObjectAnimator.AnimatorListener, LoadDataContract.View {
    private ImageView ivHeader;
    private ObjectAnimator splashImgAnim;
    private ObjectAnimator splashBtnAnim;
    private Button btnLogin;
    private RelativeLayout mainContainer;
    private int finalYOffset;
    private LoadDataContract.Presenter loadDataPresenter;
    private static final String TAG = "SplashActivity";

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        setContentView(R.layout.activity_splash);
        loadDataPresenter = new LoadDataPresenter(this, this);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        UMengUtil.getInstance().onResume(this);
        init();
    }

    @Override
    protected void onPause() {
        if (splashBtnAnim != null) {
            splashBtnAnim.removeAllListeners();
            splashBtnAnim = null;
        }
        super.onPause();
        UMengUtil.getInstance().onPause(this);
    }

    @Override
    public void initView() {
        ivHeader = (ImageView) findViewById(R.id.splash_img_head);
        btnLogin = (Button) findViewById(R.id.splash_login);
        mainContainer = (RelativeLayout) findViewById(R.id.rl_container);
        Log.d(TAG, "手机分辨率" + ScreenUtils.getScreenWidthPx(this) + "*" + ScreenUtils.getScreenHeightPx(this));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        init();
    }

    @Override
    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(SplashActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void startActivity() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //切换界面动画，头像作为共享元素
            ((ViewGroup) ivHeader.getParent()).setTransitionGroup(false);
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(SplashActivity.this, ivHeader, ivHeader.getTransitionName());
            ActivityCompat.startActivity(this, new Intent(this, SignInActivity.class), options.toBundle());
            finishAfterTransition();
        } else {
            startActivity(new Intent(this, SignInActivity.class));
            finish();
        }
    }

    @Override
    public void printLog(String msg) {
        Logger.debug(TAG, msg);
    }


    protected void init() {
        finalYOffset = ScreenUtils.getScreenHeight(this);
        splashImgAnim = AnimUtil.splashAnim(ivHeader, 1.0f, -finalYOffset / 2);
        splashBtnAnim = AnimUtil.splashAnim(btnLogin, 1.0f, (float) (-finalYOffset / 2.15));
        splashBtnAnim.setStartDelay(1500);
        splashImgAnim.setDuration(1500);

        splashBtnAnim.start();
        splashImgAnim.start();
        splashBtnAnim.addListener(this);
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (animation != null) {
            startActivity();
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    @Override
    protected void onDestroy() {
        if (splashBtnAnim != null) {
            splashBtnAnim.removeAllListeners();
            splashBtnAnim = null;
        }
        super.onDestroy();
    }


}
